// smartclass/pages/dormitoryManagementt/dormitoryManagementt.js
const _wx = require('../../../utils/util.js');
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    item:[],
    itemid: '',
    page: 1,
    warnlist: [],
    lastPage: 1,
    text: false,
    show: false,
    actions: [{
        name: '编辑',
        color: '#247AF1'
      },
      {
        name: '删除',
        color: '#ee0a24'
      },
    ],
    value: ""
  },
  handleedit(event) {
    this.setData({
      show: true
    })
    console.log(event.currentTarget.dataset.id);
    this.setData({
      itemid: event.currentTarget.dataset.id
    })
    this.setData({
      item: event.currentTarget.dataset.item
    })
  },
  handleinsert() {
    wx.navigateTo({
      url: '../../pages/adddormitoryManagementt/adddormitoryManagementt',
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  dormdormselect() {
    const that = this
    _wx.getData('admin/dorm/dorm/select', 'GET', {
      page: this.data.page,
      limit: 10,
      name: this.data.value
    }, function (res) {
      if (res.code == 0) {
        if (that.data.page == 1) {
          that.setData({
            warnlist: res.data,
            lastPage: res.lastPage,
            text: false
          })
        } else {
          that.setData({
            warnlist: that.data.warnlist.concat(res.data),
            lastPage: res.lastPage,
            text: false
          })
        }
      }
    });
  },
  onSearch() {
    this.dormdormselect()
  },
  onclear() {
    this.dormdormselect()
  },
  // 删除接口
  dormdormdelete() {
    const that = this
    _wx.getData('admin/dorm/dorm/delete', 'POST', {
      id: this.data.itemid
    }, function (res) {
      if (res.code == 0) {
        Toast.success('删除成功');
        that.dormdormselect()
      }
    });
  },
  // 调用删除接口
  onSelect(event) {
    console.log(event.detail);
    if (event.detail.name == "删除") {
      Dialog.confirm({
          title: '温馨提示',
          message: '您确定要删除当前内容',
        })
        .then(() => {
          this.dormdormdelete()
        })
        .catch(() => {
          // on cancel
        });
    } else if (event.detail.name == "编辑") {
      console.log(this.data.item);
      const queryStr = Object.keys(this.data.item).map(key => `${key}=${encodeURIComponent(this.data.item[key])}`).join('&');
      wx.redirectTo({
        url: `../editdormitoryManagementt/editdormitoryManagementt?${queryStr}`
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.dormdormselect()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.lastPage) {
      this.setData({
        text: true
      })
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    this.dormdormselect()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})